package com.broadengate.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;

import com.broadengate.bean.ContractInfo;
import com.broadengate.dao.BaseDao;
import com.broadengate.dao.ContractDao;
import com.broadengate.view.controller.LoginController;
import com.broadengate.view.model.BillModel;
import com.broadengate.view.model.ContractModel;

@Service("contractService")
public class ContractService<T> extends BaseService<T>
{
    private final static Logger log = Logger.getLogger(LoginController.class);
    
    @Autowired
    private ContractDao<T> contractDao;
    
    @Override
    public BaseDao<T> getDao()
    {
        // TODO Auto-generated method stub
        return contractDao;
    }
    
    public ContractInfo queryContractById(int id)
    {
        ContractInfo info = contractDao.queryContractById(id);
        return info;
    }
    
    public ContractInfo queryContractByPo(String po)
    {
        ContractInfo info = contractDao.queryContractByPo(po);
        return info;
    }
    
    public List<T> search(ContractModel model)
    {
        return contractDao.search(model);
    }
    
    public void updateContractInfo(ContractModel model)
    {
        contractDao.updateContractInfo(model);
    }
    
    public void addContractInfo(ContractModel model)
    {
        contractDao.addContractInfo(model);
    }
    
    public void delContracts(String contractid)
    {
        contractDao.delContracts(contractid);
    }
    
    public void addBillInfo(BillModel model)
    {
        contractDao.addBillInfo(model);
    }
    
    public void delbillInfos(String id)
    {
        contractDao.delbillInfos(id);
    }
    
    /**
     * 批量增加
     * <功能详细描述>
     * @param list [参数说明]
     * 
     * @return void [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public ServiceResult betchAddContractInfo(List<ContractModel> list)
    {
        ServiceResult result = new ServiceResult();
        result.setResult("0");
        
        List<Object> faildResult = new ArrayList();
        List<Object> successResult = new ArrayList();
        List<String> faildResultMsg = new ArrayList();
        if (list != null && list.size() > 0)
        {
            for (ContractModel contractModel : list)
            {
                //判读是否已经存在，如果存在则返回失败
                ContractInfo info = this.queryContractByPo(contractModel.getPo());
                if (info != null && info.getPo() != null)
                {
                    result.setResult("1");
                    contractModel.setExtendscloum("合同已存在");
                    faildResult.add(contractModel);
                    faildResultMsg.add("合同已存在");
                }
                else
                {
                    try
                    {
                        this.addContractInfo(contractModel);
                        successResult.add(contractModel);
                    }
                    catch (DataIntegrityViolationException e)
                    {
                        log.error(e);
                        result.setResult("1");
                        contractModel.setExtendscloum("格式错误");
                        faildResult.add(contractModel);
                        faildResultMsg.add("格式错误");
                    }
                }
            }
        }
        
        result.setFaildResult(faildResult);
        result.setFaildResultMsg(faildResultMsg);
        result.setSuccessResult(successResult);
        return result;
    }
    
    public void updateContractIsRelated(String po, String isRelated) throws Exception
    {
        Map<String, String> map = new HashMap<String, String>();
        map.put("po", po);
        map.put("isRelated", isRelated);
        contractDao.updateContractRelated(map);
    }
}
